package com.project.domain.dto.projectReviewDto;

import com.fasterxml.jackson.databind.ObjectMapper;
import com.project.domain.entity.BizReviewResult;
import lombok.Data;

import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.HashMap;

@Data
public class ReviewDetailsResDto {
////    private String bookName;
////    private String reviewNote;
////    private String reviewResult;
////    private String orderId;
//    private ArrayList<Lv1_> lv1;
////    private String orderId;
//
//    private String bookName;
//    private String applyFile;
//    private String planFile;
//    private String expertReviewId;
//    private String reviewInfo;
//    private String reviewResult;


    public static ViewExpertReviewResDto fromMybatisToResFormat(ArrayList<ViewExpertReviewJoinTableFormatDto> mysqlDataList) {
//        ReviewDetailsResDto reviewDetailsResDto = new ReviewDetailsResDto();
        ViewExpertReviewResDto viewExpertReviewResDto = new ViewExpertReviewResDto();
        ArrayList<ViewExpertReviewResLv1Dto> lv1List = new ArrayList<>();

        for (ViewExpertReviewJoinTableFormatDto mysqlLineData : mysqlDataList) {
//            Lv1_ lv1 = new Lv1_();
            ViewExpertReviewResLv1Dto viewExpertReviewResLv1Dto = new ViewExpertReviewResLv1Dto();

//            lv1.setStandardId(mysqlLineData.getStandardId());
            viewExpertReviewResLv1Dto.setStandardId(mysqlLineData.getReviewStandardId());

//            lv1.setLv1name(mysqlLineData.getLv1Name());
            viewExpertReviewResLv1Dto.setLv1name(mysqlLineData.getLv1Name());

//            lv1.setOrderId(mysqlLineData.getOrderId());
            viewExpertReviewResLv1Dto.setOrderId(mysqlLineData.getOrderId());

            // 尝试将二级分类转为json
            ObjectMapper objectMapper = new ObjectMapper();
            ArrayList<HashMap<String, String>> lv2NamesJsonList = null;
            String lv2NamesString = mysqlLineData.getLv2Names();

            if (lv2NamesString != null && !lv2NamesString.trim().isEmpty()) {
                try {
                    // 验证 JSON 有效性
                    objectMapper.readTree(lv2NamesString);
                    // 转换为 HashMap
                    lv2NamesJsonList = objectMapper.readValue(
                            lv2NamesString,
                            new com.fasterxml.jackson.core.type.TypeReference<ArrayList<HashMap<String, String>>>() {
                            }
                    );
                    ArrayList<ViewExpertReviewResLv2Dto> lv2List = new ArrayList<>();
                    for (int i = 0; i < lv2NamesJsonList.size(); i++) {
                        ViewExpertReviewResLv2Dto lv2 = new ViewExpertReviewResLv2Dto();
                        lv2.setLv2name(lv2NamesJsonList.get(i).get("value"));
                        lv2.setLv2mark(mysqlLineData.getLv2Marks().split(",")[i]);

                        if (mysqlLineData.getReviewMark()==null){
                            // 结构是json，但是没有打分
                            lv2.setReviewMark("");
                        }else {
                            // 结构是json，有打分
                            lv2.setReviewMark(mysqlLineData.getReviewMark().split(",")[i]);
                        }
                        lv2List.add(lv2);
                    }
                    viewExpertReviewResLv1Dto.setLv2(lv2List);

//                } catch (JsonMappingException e) {

//                    throw new RuntimeException("映射错误: " + e.getMessage());
                } catch (Exception e) {
                    // 纯字符数据（非json格式）
                    ArrayList<ViewExpertReviewResLv2Dto> lv2List = new ArrayList<>();
                    ViewExpertReviewResLv2Dto lv2 = new ViewExpertReviewResLv2Dto();
                    lv2.setLv2name(mysqlLineData.getLv2Names());
                    lv2.setLv2mark(mysqlLineData.getLv2Marks());
                    lv2.setReviewMark(mysqlLineData.getReviewMark()==null?"":mysqlLineData.getReviewMark());
                    lv2List.add(lv2);
                    viewExpertReviewResLv1Dto.setLv2(lv2List);
                }finally {
                    lv1List.add(viewExpertReviewResLv1Dto);
                }
            } else {
                throw new RuntimeException("JSON 字符串为空或 null，无法转换");
            }
            viewExpertReviewResDto.setLv1(lv1List);
        }
        return viewExpertReviewResDto;
    }
    public static ArrayList<BizReviewResult> fromReqFormatToMybatis(SaveExpertReviewReqDto dto){
        ArrayList<BizReviewResult> mybatisDataList = new ArrayList<>();
        String expertReviewId = dto.getExpertReviewId();
//        String reviewInfo = dto.getReviewInfo();
//        String reviewResult = dto.getReviewResult();
        ArrayList<ViewExpertReviewResLv1Dto> lv1List = dto.getLv1();
        for (ViewExpertReviewResLv1Dto lv1 :lv1List) {
            String standardId = lv1.getStandardId();
            String lv1name = lv1.getLv1name();//该字段不用保存到biz_review_result中
            Integer orderId = lv1.getOrderId();//该字段不用保存到biz_review_result中

            ArrayList<String> realMarlTmpList = new ArrayList<>();

            ArrayList<ViewExpertReviewResLv2Dto> lv2List = lv1.getLv2();
            for (ViewExpertReviewResLv2Dto lv2 :lv2List) {
                realMarlTmpList.add(lv2.getReviewMark());
            }
            //计算总分
            BigDecimal reviewMark = BigDecimal.ZERO;
            for (String num : realMarlTmpList) {
                BigDecimal value = new BigDecimal(num);
                reviewMark = reviewMark.add(value);
            }

            BizReviewResult mybatisData = new BizReviewResult();
            mybatisData.setExpertReviewId(expertReviewId);
            mybatisData.setReviewStandardId(standardId);
            mybatisData.setReviewMark(reviewMark);
            mybatisData.setLv2ReviewMarks(String.join(",",realMarlTmpList));
            mybatisDataList.add(mybatisData);
        }
        return mybatisDataList;
    }
}

//@Data
//class Lv1_ {
//    private String standardId;
//    private String lv1name;
////    private String lv1mark;
//    private Integer orderId;
//    private ArrayList<Lv2_> lv2;
////    private String lvl2Names;
////    private String lvl2Marks;
//}
//
//@Data
//class Lv2_ {
//    private String lv2name;
//    private String lv2mark;
//    private String reviewMark;//真实评分
//}



